package finalproject.util.datastructure;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import finalproject.model.items.LabItem;
import finalproject.model.items.LectureItem;

public class LectureLabAssociation implements Association<LectureItem, LabItem, String>

{
	private Map<LectureItem,LabItem> workingItems;

	public LectureLabAssociation()
	{
		this.workingItems = new HashMap<LectureItem,LabItem>();
	}


	@Override
	public boolean addKey(LectureItem item)
	{
		if(item == null)
		{
			return false;
		}
		if(!containsKey(item))
		{
			workingItems.put(item,null);
			return true;
		}
		return false;
	}


	@Override
	public boolean addValue(LabItem lab)
	{
		if(lab == null)
		{
			return false;
		}
		else if(containsValue(lab))
		{
			return false;
		}
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		while(it.hasNext())
		{
			current = it.next();
			if(matchs(current, lab))
			{
				workingItems.put(current, lab);
				return true;
			}
		}
		return false;
	}


	@Override
	public boolean isComplete()
	{
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		while (it.hasNext()) {
			current = it.next();
			if(!matchs(current,workingItems.get(current)))
			{
				return false;
			}
		}
		return true;
	}

	public static boolean matchs(LectureItem lectItem, LabItem labItem)
	{
		if(lectItem == null || labItem == null)
		{
			return false;
		}
		if(lectItem.getLecture().getLabSession().getTitle().equals(labItem.getLabSession().getTitle()))
		{
			return true;
		}
		return false;
	}


	@Override
	public boolean containsKey(LectureItem item)
	{   
		return workingItems.containsKey(item);
	}


	@Override
	public boolean containsValue(LabItem item)
	{
		return workingItems.containsValue(item);
	}


	@Override
	public boolean eraseKey(LectureItem lecture) 
	{
		if (workingItems.remove(lecture) == null)
		{
			return false;
		}
		else
		{
			return true;
		}

	}

	@Override
	public boolean eraseValue(LabItem lab) 
	{
		if(lab == null)
		{
			return false;
		}
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		while (it.hasNext()) 
		{
			current = it.next();

			if(matchs(current, lab))
			{
				workingItems.put(current, null);
				return true;
			}
		}
		return false;
	}


	@Override
	public LectureItem getKeyWith(String specificator) 
	{
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		while(it.hasNext())
		{
			current = it.next();
			if(current.getLecture().getTitle().equals(specificator))
			{
				return current;
			}
		}
		return null;  
	}


	@Override
	public LabItem getValueWith(String specificator) 
	{
		try
		{
			Iterator<LectureItem> it = workingItems.keySet().iterator();
			LectureItem current;
			while(it.hasNext())
			{
				current = it.next();
				if(current != null)
				{
					if(workingItems.get(current).getLabSession().getTitle().equals(specificator))
					{
						return workingItems.get(current);
					}
				}
			}
			return null;  
		}
		catch(Exception e)
		{
			return null;
		}
	}


	@Override
	public LectureItem getKey(int i) {
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		int j=0;
		while(it.hasNext())
		{
			current = it.next();
			if(i == j)
			{
				return current;
			}
			j++;
		}
		return null;
	}


	@Override
	public LabItem getValue(int i) {
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		int j=0;
		while(it.hasNext())
		{
			current = it.next();
			if(i == j)
			{
				return workingItems.get(current);
			}
			j++;
		}
		return null;
	}


	@Override
	public int size() {
		return workingItems.size();
	}


	@Override
	public boolean containskeyOf(String value) {
		Iterator<LectureItem> it = workingItems.keySet().iterator();
		LectureItem current;
		while(it.hasNext())
		{
			current = it.next();
			if(current.getLecture().getLabSession().getTitle().equals(value))
			{
				return true;
			}
		}
		return false;
	}



}
